package app.tuwien.isi.tweetanalyser;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;

public class Tweet implements Parcelable{
    private long id;
    private String text;
    private String source;
    private String place;
    private String placeType;
    private String country;
    private String coordinates;
    private String time;
    private Date date;
    private String replyToStatus;
    private String replyToUser;
    private int retweetCount;
    
    private long userID;
    private String userName;
    private String profileImage;
    private Drawable image;
    private String userLocation;
    private String language;
    private int followers;
    
    /**
     * 
     * @param object
     * @param actionCode	0...user_timeline
     * 						1...query
     * @throws JSONException
     */
    public Tweet(JSONObject object, int actionCode) throws JSONException {
    	if(actionCode == 0){
    		//Tweet-Information
	        id = object.getLong("id");
	        text = object.getString("text");
	        source = object.getString("source");
	        
	        if(object.isNull("place")){
	        	place = null;
	        	placeType = null;
	        	country = null;
	        }else{
	        	JSONObject placeObject = object.getJSONObject("place");
	        	place = placeObject.getString("full_name");
	        	placeType = placeObject.getString("place_type");
	        	country = placeObject.getString("country");
	        }
	        
	        if(object.isNull("geo")){
	        	setCoordinates(null);
	        }else{
	        	String temp = object.getJSONObject("geo").getString("coordinates");
	        	setCoordinates(temp.substring(1, temp.length()-1));
	        }
	        
	        try {
	            SimpleDateFormat sdfToDate = new SimpleDateFormat(
	                    "EEE MMM dd HH:mm:ss Z yyyy");
	            date = sdfToDate.parse(object.getString("created_at"));
	        } catch (Exception ex2) {
	            ex2.printStackTrace();
	        }

	        time = object.getString("created_at");
	        replyToUser = object.getString("in_reply_to_user_id");
	        replyToStatus = object.getString("in_reply_to_status_id");
	        retweetCount = object.getInt("retweet_count");
	        
	        //User-Information
	        JSONObject userObject = object.getJSONObject("user");
	        userID = userObject.getLong("id");
	        userName = userObject.getString("name");
	        profileImage = userObject.getString("profile_image_url");
	        userLocation = userObject.getString("location");
	        language = userObject.getString("lang");
	        followers = userObject.getInt("followers_count");
	        
	        try{
	            String url = profileImage;           
	            image = ImageOperations(url);
	        }
	        catch(Exception ex)
	        {
	            ex.printStackTrace();
	        }
	        
    	}else{
    		if(actionCode == 1){
    			id = object.getLong("id");
    			text = object.getString("text");
    			source = object.getString("source");
    			//TODO GEO
    			try {
    	            SimpleDateFormat sdfToDate = new SimpleDateFormat(
    	                    "EEE MMM dd HH:mm:ss Z yyyy");
    	            date = sdfToDate.parse(object.getString("created_at"));
    	        } catch (Exception ex2) {
    	            ex2.printStackTrace();
    	        }
    			
    			time = object.getString("created_at");
    			userID = object.getLong("from_user_id");
    	        userName = object.getString("from_user_name");
    		}
    	}
    }

	public long getId() {
        return id;
    }
    public String getText() {
        return text;
    }
    public String getPlace() {
        return place;
    }
    public String getUserName() {
        return userName;
    }
    public String getSource() {
        return source;
    }
    public String getProflieImage() {
        return profileImage;
    }
    public Drawable getImage() {
        return image;
    }
    public String getLocation() {
    	return userLocation;
    }
    public String getCoordinates() {
		return coordinates;
	}
    private void setCoordinates(String coordinates) {
		this.coordinates = coordinates;
	}
	public String getPlaceType() {
		return placeType;
	}
	public String getCountry() {
		return country;
	}
	public String getTime() {
		return time;
	}
	public Date getDate() {
		return date;
	}
	public String getReplyToStatus() {
		return replyToStatus;
	}
	public String getReplyToUser() {
		return replyToUser;
	}
	public int getRetweetCount() {
		return retweetCount;
	}
//	public boolean isRetweet() {
//		return retweet;
//	}
//	public boolean isFavorited() {
//		return favorited;
//	}
	public long getUserID() {
		return userID;
	}
	public String getUserLocation() {
		return userLocation;
	}
	public int getFollowers() {
		return followers;
	}
	public String getLanguage() {
		return language;
	}
    
	public void addProperties(String userName, String profileImage, String userLocation, String language, int followers){
		this.userName = userName;
		this.profileImage = profileImage;
		this.userLocation = userLocation;
		this.language = language;
		this.followers = followers;
		
		try{
            String url = profileImage;           
            this.image = ImageOperations(url);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
	}
	
    private Drawable ImageOperations(String url) {
        try {
            InputStream is = (InputStream) this.fetch(url);
            Drawable d = Drawable.createFromStream(is, "src");
            return d;
        } catch (MalformedURLException e) {
            return null;
        } catch (IOException e) {
            return null;
        }
    }
    
    public Object fetch(String address) throws MalformedURLException,
    IOException {
        URL url = new URL(address);
        Object content = url.getContent();
        return content;
    }
    
	public int describeContents() {
		return 0;
	}
	
	public void writeToParcel(Parcel out, int flag) {
		out.writeLong(id);
	    out.writeString(text);
	    out.writeString(source);
	    out.writeString(place);
	    out.writeString(placeType);
	    out.writeString(country);
	    out.writeString(coordinates);
	    out.writeString(time);
	    out.writeString(replyToStatus);
	    out.writeString(replyToUser);
	    out.writeInt(retweetCount);
//	    out.writeString(retweet);
//	    out.writeString(favorited);
	    	
	    out.writeLong(userID);
	    out.writeString(userName);
	    out.writeString(profileImage);
	    out.writeString(userLocation); 
	    out.writeString(language); 
	    out.writeInt(followers); 
	}
	
	public static final Parcelable.Creator<Tweet> CREATOR = new
			  Parcelable.Creator<Tweet>() {
	      public Tweet createFromParcel(Parcel source) {
	            return new Tweet(source);
	      }
	      public Tweet[] newArray(int size) {
	            return new Tweet[size];
	      }
	};
	
	/**
     * This will be used only by the CREATOR
     * and reconstruct the tweets from the Parcel
     * @param in ... to reconstruct the tweet
     */
	public Tweet(Parcel in){
		id = in.readLong();
		text = in.readString();
		source = in.readString();
		place = in.readString();
		placeType = in.readString();
		country = in.readString();
		coordinates = in.readString();
		time = in.readString();
		replyToStatus = in.readString();
	    replyToUser = in.readString();
	    retweetCount = in.readInt();
//	    retweet = in.;
//	    favorited = in.;
		
	    userID = in.readLong();
	    userName = in.readString();
		profileImage = in.readString();
		userLocation = in.readString();
		language = in.readString();
		followers = in.readInt();
		
		try{
            String url = profileImage;           
            image = ImageOperations(url);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
}

